package com.gxuwz.business.action.front;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.business.action.BaseAction;
import com.gxuwz.business.entity.SysUser;
import com.gxuwz.business.service.ISysUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class LoginAction extends BaseAction implements Preparable,ModelDriven<SysUser>{
	
	protected static final String LOGIN_JSP = "/login.jsp";
	protected static final String WELCOME_JSP = "/main.html";
	private Logger log = Logger.getLogger(getClass());
	
	private SysUser user;
	
	//登录错误信息
	String errorMessage = "";
	
	@Autowired
	private ISysUserService sysUserService;
	
	public void security(){
		System.out.println("不会被执行");
	}
	
	public String webLogin(){
		//是否允许登陆
		boolean isLogin = false;
		isLogin = sysUserService.isLogin(user);
		System.out.println("isLogin："+isLogin);
		//是否有此用户
//		log.info(user);
		user = sysUserService.findByName(user.getUserName());

		if(isLogin){
			ActionContext.getContext().getSession().put("user", user);
		}else{
			//查不到用户
			if(null == user){
				errorMessage = "此用户不存在";
			}else if(null != user){
				errorMessage = "用户名或密码错误";
			}
			log.error(errorMessage);
			ActionContext.getContext().getSession().put("errorMessage", errorMessage);
			forwardView = LOGIN_JSP;
		}
		return "security";
	}
	
	/**
	 * 打开欢迎界面
	 */
	public String welcome(){
		forwardView = WELCOME_JSP;
		return SUCCESS;
	}
	

	@Override
	public SysUser getModel() {
		return user;
	}


	@Override
	public void prepare() throws Exception {
		if(user == null){
			user = new SysUser();
		}
		
	}
	
	
	
	
}
